package com.xsjl.spit.controller;


import com.xsjl.common.entity.PageResult;
import com.xsjl.common.entity.Result;
import com.xsjl.common.entity.StatusCode;
import com.xsjl.common.utils.JwtUtil;
import com.xsjl.spit.pojo.Spit;
import com.xsjl.spit.service.SpitService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {
    @Autowired
    private SpitService spitService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtUtil jwtUtil;


    @GetMapping
    public Result spitList() {
        return new Result(true, StatusCode.OK, "查询成功", spitService.findSpit());
    }

    @GetMapping(value = "/{spitId}")
    public Result getSpitById(@PathVariable String spitId) {
        return new Result(true, StatusCode.OK, "查询成功", spitService.findSpitById(spitId));
    }

    @PutMapping(value = "/{spitId}")
    public Result updateSpit(@PathVariable String spitId, @RequestBody Spit spit) {
        Claims claims = (Claims) request.getAttribute("user_claims");
        if (claims == null) {
            return new Result(false, StatusCode.ACCESSERROR, "权限不足");
        }
        spit.setId(spitId);
        spitService.updateSpit(spit);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @DeleteMapping(value = "/{spitId}")
    public Result deleteSpit(@PathVariable String spitId) {
        Claims claims = (Claims) request.getAttribute("user_claims");
        if (claims == null) {
            return new Result(false, StatusCode.ACCESSERROR, "权限不足");
        }
        spitService.deleteSpitById(spitId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @PostMapping
    public Result addSpit(@RequestBody Spit spit) {
        Claims claims = (Claims) request.getAttribute("user_claims");
        if (claims == null) {
            return new Result(false, StatusCode.ACCESSERROR, "权限不足");
        }
        spitService.saveSpit(spit);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @GetMapping(value = "/comment/{parentid}/{page}/{size}")
    public Result findSpitByParentid(@PathVariable String parentid, @PathVariable int page, @PathVariable int size) {
        Page<Spit> spitPage = spitService.findSpitByParentid(parentid, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Spit>(spitPage.getTotalElements(), spitPage.getContent()));
    }

    @PutMapping(value = "/thumbup/{spitId}")
    public Result addThumbup(@PathVariable String spitId) {
        Claims claims = (Claims) request.getAttribute("user_claims");
        if (claims == null) {
            return new Result(false, StatusCode.ACCESSERROR, "权限不足");
        }
        // 需要判断用户是否已经点过赞,此时需要通过token来获取用户id
        String userid = claims.getId();
        //String userid="2";
        if (redisTemplate.opsForValue().get("thumbup_" + userid + "spitId"+ spitId) != null) {
            return new Result(false, StatusCode.REPERROR, "你已经点过赞了");
        }
        redisTemplate.opsForValue().set("thumbup_" + userid + "spitId"+ spitId, 1, 24, TimeUnit.HOURS);
        spitService.addThumbup(spitId);
        return new Result(true, StatusCode.OK, "点赞成功");
    }

}
